/*Copyright (C) $today.year  深圳极向量科技有限公司 All Rights Reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

package neatlogic.module.change.notify.handler.param;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import neatlogic.framework.change.constvalue.ChangeTriggerType;
import neatlogic.framework.crossover.CrossoverServiceFactory;
import neatlogic.framework.notify.core.INotifyTriggerType;
import neatlogic.framework.process.crossover.IProcessTaskCrossoverMapper;
import neatlogic.framework.process.dto.ProcessTaskStepVo;
import neatlogic.framework.process.exception.process.ProcessStepHandlerNotFoundException;
import neatlogic.framework.process.notify.core.ProcessTaskNotifyParamHandlerBase;
import neatlogic.framework.process.stephandler.core.IProcessStepInternalHandler;
import neatlogic.framework.process.stephandler.core.ProcessStepInternalHandlerFactory;
import neatlogic.module.change.notify.constvalue.ChangeHandleNotifyParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

/**
 * @author linbq
 * @since 2021/10/16 15:52
 **/
@Component
public class ChangeStepNameParamHandler extends ProcessTaskNotifyParamHandlerBase {

    @Override
    public String getValue() {
        return ChangeHandleNotifyParam.CHANGESTEPNAME.getValue();
    }

    @Override
    public Object getMyText(ProcessTaskStepVo processTaskStepVo, INotifyTriggerType notifyTriggerType) {
        if (!(notifyTriggerType instanceof ChangeTriggerType)) {
            return null;
        }
        JSONObject paramObj = processTaskStepVo.getParamObj();
        Long changeStepId = paramObj.getLong("changeStepId");
        if (changeStepId != null) {
            IProcessTaskCrossoverMapper processTaskCrossoverMapper = CrossoverServiceFactory.getApi(IProcessTaskCrossoverMapper.class);
            ProcessTaskStepVo stepVo = processTaskCrossoverMapper.getProcessTaskStepBaseInfoById(processTaskStepVo.getId());
            IProcessStepInternalHandler handler = ProcessStepInternalHandlerFactory.getHandler(stepVo.getHandler());
            if (handler == null) {
                throw new ProcessStepHandlerNotFoundException(stepVo.getHandler());
            }
            Object handlerStepInfo = handler.getNonStartStepInfo(stepVo);
            if (handlerStepInfo != null) {
                JSONObject jsonObj = (JSONObject) JSON.toJSON(handlerStepInfo);
                JSONArray changeStepList = jsonObj.getJSONArray("changeStepList");
                if (CollectionUtils.isNotEmpty(changeStepList)) {
                    for (int i = 0; i < changeStepList.size(); i++) {
                        JSONObject changeStepObj = changeStepList.getJSONObject(i);
                        if (changeStepId.equals(changeStepObj.getLong("id"))) {
                            return changeStepObj.getString("name");
                        }
                    }
                }
            }
        }
        return null;
    }
}
